package com.zzy.util.awt;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Random;

/**
 * 防止 电脑锁屏
 * 模拟键盘 按钮，防止锁屏
 */
public class PreventScreenLock {
    public static void main(String[] args) {
        preventScreenLockMethod();
    }
    public static void preventScreenLockMethod(){
        try {
            Robot robot = new Robot();
            while (true){
                // 按一次 键盘键  SCROLL_LOCK  NUM_LOCK 这两个对 操作 影响最小
                robot.keyPress(KeyEvent.VK_SCROLL_LOCK);
                robot.keyRelease(KeyEvent.VK_SCROLL_LOCK);
                //等待 1 到 2 秒
                Thread.sleep( randomNum(1,2) * 1000 );
                //在按一次键盘
                robot.keyPress(KeyEvent.VK_SCROLL_LOCK);
                robot.keyRelease(KeyEvent.VK_SCROLL_LOCK);

                //根据自己的 需要 调整下一次 按键 时间
                Thread.sleep( randomNum(5,10) * 1000 );


            }


        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static int randomNum(int min,int max){
        Random random = new Random();
        int num = random.nextInt(max)%(max - min + 1) + min;
        return  num;
    }
}
